package www.wxwnb.clerk.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import www.wxwnb.clerk.R;
import www.wxwnb.clerk.utils.AppManager;


/**
 * mqq
 * Created by xcd15 on 2017/5/3.
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "BaseActivity";
    private boolean isActive = true;
    private UploadProgressDialog mUploadProgressDialog;
    private UploadProgressDialog mUploadPD;
    private ProgressDialog mDefaultProgressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       /* if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }*/
        super.onCreate(savedInstanceState);
        AppManager.getInstance().addActivity(this);
        mUploadProgressDialog = new UploadProgressDialog(this, R.style.UploadDialog);

    }

    protected abstract void initView();


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

    }

    public abstract void initListener();

    public abstract void initData();

    /**
     * 获取当前页面需要的权限
     *
     * @return
     */
    protected String[] getPermissions() {
        return null;
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        isActive = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        isActive = false;
    }

    public boolean activityIsActivity() {
        return isActive;
    }

    @Override
    public void finish() {
        super.finish();
        AppManager.getInstance().removeActivity(this);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        afterSetContentView();
        initView();
        initData();
        initListener();
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
        afterSetContentView();
        initView();
        initData();
        initListener();
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        afterSetContentView();
        initView();
        initData();
        initListener();
    }


    protected void afterSetContentView() {
    }
    /**
     * 根据resId获得一个Bitmap
     * @param resId
     * @return
     */
    public Bitmap getDrawableBitmap(int resId) {
        Drawable drawable = getResources().getDrawable(resId);
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            return bitmapDrawable.getBitmap();
        } else {
            return null;
        }
    }

    public void intentActivity(Context context, Class<?> taretClass) {
        startActivity(new Intent(context, taretClass));
    }

    public void intentActivity(Context context, Bundle bundle, Class<?> taretClass) {
        Intent intent = new Intent(context, taretClass);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        onDestroyDeatchView();
    }

    protected void onDestroyDeatchView() {
    }

    @Override
    public void onClick(View v) {
        processClick(v);
    }

    public abstract void processClick(View v);



    public void showDefaultProgressDialog(String title, String msg, boolean indeterminate, boolean cancelable) {
        mDefaultProgressDialog = ProgressDialog.show(this, title, msg, indeterminate, cancelable);
    }

    public void dismissDefaultProgressDialog() {
        mDefaultProgressDialog.dismiss();
    }

    public void backgroundAlpha(float alp) {
        WindowManager.LayoutParams attributes = this.getWindow().getAttributes();
        attributes.alpha = alp;
        this.getWindow().setAttributes(attributes);
    }

    public void showUploadDialog() {
        if (!this.isFinishing() && mUploadProgressDialog != null && !mUploadProgressDialog.isShowing()) {
            mUploadProgressDialog.show();
            backgroundAlpha(0.6f);
        } else if (mUploadProgressDialog == null) {
            mUploadProgressDialog = new UploadProgressDialog(this, R.style.UploadDialog);
            mUploadProgressDialog.show();
            backgroundAlpha(0.6f);
        }
    }

    public void dismissUploadDialog() {
        if (!this.isFinishing() && mUploadProgressDialog != null && mUploadProgressDialog.isShowing()) {
            mUploadProgressDialog.dismiss();
            backgroundAlpha(1.0f);
        }
    }

    /**
     * 把格式化了的string转换成时间戳
     *
     * @param text
     * @return
     */
    public String getDateString(String text) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parseDate = null;
        try {
            parseDate = simpleDateFormat.parse(text);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        long time = parseDate.getTime();
        return String.valueOf(time);
    }
    public void showUploadProgressDialog() {
        if (mUploadPD == null) {
            mUploadPD = new UploadProgressDialog(this, R.style.UploadDialog);
        }
        mUploadPD.show();
        backgroundAlpha(0.6f);
    }

    public void dismissUploadProgressDialog() {
        if (mUploadPD != null && mUploadPD.isShowing()) {
            mUploadPD.dismiss();
        }
        backgroundAlpha(1.0f);
    }

}
